package com.naiterui.ehp.bs.doctor.repository;

import com.naiterui.common.repository.core.BaseJpaRepository;
import com.naiterui.ehp.bp.domain.Hospital;
import com.naiterui.ehp.bp.domain.PatientPersonDoctor;
import com.naiterui.ehp.bp.domain.PatientPersonDoctorPK;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 医患关系实体dao
 *
 * @author wangzehao
 */
@Repository
public interface IPatientPersonDoctorRepository extends BaseJpaRepository<PatientPersonDoctor, PatientPersonDoctorPK>  {

    @Query("FROM PatientPersonDoctor WHERE personPK.doctorId=?1 AND personPK.patientId IN (?2)")
    List<PatientPersonDoctor> getByPatientIds(Long doctorId, Collection<Long> patientId);



    @Query("from PatientPersonDoctor where relationFlag = ?1 and personPK.doctorId=?2")
    List<PatientPersonDoctor> getPatientIdsByDoctorId(int relationFlag, Long doctorId);

    @Query(value = "from PatientPersonDoctor where relationFlag = 0 and  personPK.doctorId=?1 and createAt >= ?2 order by createAt desc",
     countQuery = "select count(t) from PatientPersonDoctor t where t.relationFlag = 0 and t.personPK.doctorId=?1 and t.createAt >= ?2")
    Page<PatientPersonDoctor> getByPeriods(Long doctorId, Date startTime, Pageable pageable) ;

    @Query(value = "from PatientPersonDoctor where relationFlag = ?1 and personPK.doctorId=?2 order by createAt desc",
            countQuery = "select count(t) from PatientPersonDoctor t where t.relationFlag = ?1 and t.personPK.doctorId=?2")
    Page<PatientPersonDoctor> getByPeriods(Integer relationFlag, Long doctorId, Pageable pageable);


    @Query(value = "from PatientPersonDoctor where personPK.doctorId=?1 order by createAt desc",
            countQuery = "select count(t) from PatientPersonDoctor t where t.personPK.doctorId=?1")
    Page<PatientPersonDoctor> getByPeriods(Long doctorId, Pageable pageable);


    /*@Query(value = "select ppd.personPK.patientId as patientId, ppd.personPK.doctorId as doctorId, ppd.inquirerId as inquirerId, ppd.source as source, " +
            "ppd.createAt as createTime, pii.name as name, pii.gender as gender, pii.birthday as birthday, pii.idCard as idCard, p.name as patientName " +
            "from PatientPersonDoctor ppd " +
            "left join PatientInquirerInfo pii on pii.id = ppd.inquirerId " +
            "left join Patient p on p.id = ppd.personPK.patientId " +
            "where ppd.personPK.doctorId = :doctorId and ppd.relationFlag = :relationFlag " +
            "and p.accountStatus = 0 and pii.status = 1 order by ppd.createAt desc")
    Page<Map<String,Object>> queryDoctorPatient(@Param("doctorId") Long doctorId, @Param("relationFlag") Integer relationFlag, Pageable pageable);*/

    @Query("from PatientPersonDoctor where personPK.patientId=?1")
    List<PatientPersonDoctor> getDoctorIdsByPatientId(Long patientId);

    /**
     * 获取医生患者总数
     *
     * @param doctorId
     *
     * @return
     */
    @Query("select count(t) from PatientPersonDoctor t where relationFlag = 0 and personPK.doctorId = ?1 " )
     Integer getTotalPatientCount(Long doctorId) ;

    /**
     * 初始化医生今日患者数
     *
     * @param doctorId
     * @param date
     *
     * @return
     */
    @Query( "select count(t) from PatientPersonDoctor t where relationFlag = 0 and personPK.doctorId = ?1 and createAt  > ?2 ")
    Long getTodayPatientCount(Long doctorId, Date date) ;

    @Query(value = "select count(*) num from patient_personal_doctor where relation_flag = 0 and doctor_id = ?1 and DATE_FORMAT(NOW(), '%Y-%m-%d') = DATE_FORMAT(create_at, '%Y-%m-%d')",
            nativeQuery = true)
    Long getTodayPatientCountNew(Long doctorId);
}
